﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.Caching;

namespace BO
{
    public class CacheUtil
    {
        //CacheDependency
        public static string DependencyName = "KhodepCache";
        public static void addCache(object obj, string keyCache, string tableOfSql, int Minutes)
        {
            //giay
            //tao cache
            SqlCacheDependency dep = new SqlCacheDependency(DependencyName, tableOfSql);
            HttpContext.Current.Cache.Insert(keyCache, obj, dep, DateTime.Now.AddMinutes(Minutes), Cache.NoSlidingExpiration);
        }
        public static void addCacheThanOne(object obj, string keyCache, SqlCacheDependency dep1, AggregateCacheDependency aggDep,int Minutes)
        {
            //tao cache
            /*
            SqlCacheDependency dep1 = new SqlCacheDependency("MyDb", "Users"),
                dep2 = new SqlCacheDependency("MyDb", "UserRoles"),
                dep3 = new SqlCacheDependency("MyDb", "Policies");

            AggregateCacheDependency aggDep = new AggregateCacheDependency();
            aggDep.Add(dep1, dep2, dep3);
            */
            HttpContext.Current.Cache.Insert(keyCache, obj, aggDep, DateTime.Now.AddMinutes(Minutes),Cache.NoSlidingExpiration);
        }
        public static void removeCache(string keyCache)
        {
            //tao cache
            HttpContext.Current.Cache.Remove(keyCache);
        }
        public static object getCache(string keyCache)
        {
            object obj;
            try
            {
                obj= HttpContext.Current.Cache[keyCache];
            }
            catch (Exception)
            {

                obj= null;
            }
            return obj;
        }
        //end CacheDependency

        //memcache
        public static void Add(string key, object obj)
        {
            //SharedCacheWrapper.Add(key, obj);
            HttpContext.Current.Cache[key] = obj;

        }
        public static void Add(string key, object obj, int minutes)
        {
            //SharedCacheWrapper.Add(key, obj, minutes);
            // HttpContext.Current.Cache[key] = obj;
            // HttpContext.Current.Cache.Add(key, obj, null, DateTime.Now.AddMinutes(minutes), new TimeSpan(0, minutes, 0), CacheItemPriority.Default, null);
            HttpContext.Current.Cache.Insert(key, obj, null, DateTime.Now.AddMinutes(minutes), Cache.NoSlidingExpiration);
        }
        public static void Remove(string key)
        {
            try
            {
                HttpContext.Current.Cache.Remove(key);
            }
            catch { }
        }
        public static object Get(string key)
        {
            return HttpContext.Current.Cache[key];
            //return SharedCacheWrapper.Get(key);
        }

        //cache cả trang
        public static void CachePage(HttpResponse response, int minutes)
        {
            // Only cache for X seconds.
            response.Cache.SetExpires(DateTime.Now.AddMinutes(minutes));
            response.Cache.SetMaxAge(new TimeSpan(0, minutes, 0));
            response.Cache.SetCacheability(HttpCacheability.Public);
            response.Cache.SetValidUntilExpires(true);

            // Sliding expiration means we reset the X seconds after each request.
            // SetETag is required to work around one aspect of sliding expiration.
            response.Cache.SetSlidingExpiration(true);
            response.Cache.SetETagFromFileDependencies();
        }
        //
    }
}
